<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>【Druid】SpringBoot配置Druid监控检测慢SQL | ChenWeiHui's Blog</title><meta name="author" content="cwh"><meta name="copyright" content="cwh"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="概况Druid是阿里巴巴生态中的一员，不仅提供了高效的数据库连接池，还包括了SQL解析和数据源监控功能。在系统中遇到慢SQL问题时，借助Druid的监控能力，可以快速地定位问题所在，从而提高系统的性能和效率。   Github地址：https:&#x2F;&#x2F;github.com&#x2F;alibaba&#x2F;druid  项目框架SpringBoot + Mybatis-Plus + MySQL8 + druid 引入1">
<meta property="og:type" content="article">
<meta property="og:title" content="【Druid】SpringBoot配置Druid监控检测慢SQL">
<meta property="og:url" content="https://gitee.com/CWH6/2023/10/13/%E3%80%90Druid%E3%80%91SpringBoot%E9%85%8D%E7%BD%AEDruid%E7%9B%91%E6%8E%A7%E6%A3%80%E6%B5%8B%E6%85%A2SQL/index.html">
<meta property="og:site_name" content="ChenWeiHui&#39;s Blog">
<meta property="og:description" content="概况Druid是阿里巴巴生态中的一员，不仅提供了高效的数据库连接池，还包括了SQL解析和数据源监控功能。在系统中遇到慢SQL问题时，借助Druid的监控能力，可以快速地定位问题所在，从而提高系统的性能和效率。   Github地址：https:&#x2F;&#x2F;github.com&#x2F;alibaba&#x2F;druid  项目框架SpringBoot + Mybatis-Plus + MySQL8 + druid 引入1">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://cwh6-bucket.oss-cn-shanghai.aliyuncs.com/bk/xhs.png">
<meta property="article:published_time" content="2023-10-13T15:24:20.000Z">
<meta property="article:modified_time" content="2023-10-15T15:39:27.830Z">
<meta property="article:author" content="cwh">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cwh6-bucket.oss-cn-shanghai.aliyuncs.com/bk/xhs.png"><link rel="shortcut icon" href="/bk/img/jljy3.png"><link rel="canonical" href="https://gitee.com/CWH6/2023/10/13/%E3%80%90Druid%E3%80%91SpringBoot%E9%85%8D%E7%BD%AEDruid%E7%9B%91%E6%8E%A7%E6%A3%80%E6%B5%8B%E6%85%A2SQL/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/bk/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/bk/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '【Druid】SpringBoot配置Druid监控检测慢SQL',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-10-15 23:39:27'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://cwh6-bucket.oss-cn-shanghai.aliyuncs.com/bk/xhs.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/bk/archives/"><div class="headline">Articles</div><div class="length-num">66</div></a><a href="/bk/tags/"><div class="headline">Tags</div><div class="length-num">33</div></a><a href="/bk/categories/"><div class="headline">Categories</div><div class="length-num">8</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/bk/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/bk/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/bk/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/bk/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> List</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/bk/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/bk/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/bk/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/bk/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/bk/img/alone_blue_sky.jpg')"><nav id="nav"><span id="blog-info"><a href="/bk/" title="ChenWeiHui's Blog"><span class="site-name">ChenWeiHui's Blog</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/bk/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/bk/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/bk/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/bk/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> List</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/bk/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/bk/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/bk/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/bk/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">【Druid】SpringBoot配置Druid监控检测慢SQL</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2023-10-13T15:24:20.000Z" title="Created 2023-10-13 23:24:20">2023-10-13</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2023-10-15T15:39:27.830Z" title="Updated 2023-10-15 23:39:27">2023-10-15</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="【Druid】SpringBoot配置Druid监控检测慢SQL"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="概况"><a href="#概况" class="headerlink" title="概况"></a>概况</h2><p><code>Druid</code>是阿里巴巴生态中的一员，不仅提供了高效的数据库连接池，还包括了SQL解析和<code>数据源监控</code>功能。在系统中遇到慢SQL问题时，借助Druid的监控能力，可以快速地定位问题所在，从而提高系统的性能和效率。</p>
<blockquote>
<p> Github地址：<a target="_blank" rel="noopener" href="https://github.com/alibaba/druid">https://github.com/alibaba/druid</a></p>
</blockquote>
<h2 id="项目框架"><a href="#项目框架" class="headerlink" title="项目框架"></a>项目框架</h2><p><strong>SpringBoot + Mybatis-Plus + MySQL8 + druid</strong></p>
<h2 id="引入"><a href="#引入" class="headerlink" title="引入"></a>引入</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;dependency&gt;</span><br><span class="line">       &lt;groupId&gt;com.alibaba&lt;/groupId&gt;</span><br><span class="line">       &lt;artifactId&gt;druid-spring-boot-starter&lt;/artifactId&gt;</span><br><span class="line">       &lt;version&gt;<span class="number">1.2</span><span class="number">.16</span>&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>

<h2 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h2><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">datasource:</span></span><br><span class="line">    <span class="attr">driver-class-name:</span> <span class="string">com.mysql.cj.jdbc.Driver</span></span><br><span class="line">    <span class="attr">url:</span> <span class="string">jdbc:mysql://127.0.0.1:3306/z-self?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai&amp;allowMultiQueries=true</span></span><br><span class="line">    <span class="attr">username:</span> <span class="string">root</span></span><br><span class="line">    <span class="attr">password:</span> <span class="number">123456</span></span><br><span class="line">    <span class="attr">druid:</span></span><br><span class="line">      <span class="comment"># 初始化连接池大小</span></span><br><span class="line">      <span class="attr">initial-size:</span> <span class="number">5</span></span><br><span class="line">      <span class="comment"># 连接池最小空闲数</span></span><br><span class="line">      <span class="attr">min-idle:</span> <span class="number">5</span></span><br><span class="line">      <span class="comment"># 连接池最大连接数</span></span><br><span class="line">      <span class="attr">max-active:</span> <span class="number">20</span></span><br><span class="line">      <span class="comment"># 连接时最大等待时间（单位：毫秒）</span></span><br><span class="line">      <span class="attr">max-wait:</span> <span class="number">60000</span></span><br><span class="line">      <span class="comment"># 检测关闭空闲连接的时间间隔（单位：毫秒）</span></span><br><span class="line">      <span class="attr">time-between-eviction-runs-millis:</span> <span class="number">60000</span></span><br><span class="line">      <span class="comment"># 保持空闲连接不被关闭的最小生存时间（单位：毫秒）</span></span><br><span class="line">      <span class="attr">min-evictable-idle-time-millis:</span> <span class="number">300000</span></span><br><span class="line">      <span class="comment"># 检测连接有效的SQL</span></span><br><span class="line">      <span class="comment"># 为空则test-while-idle、test-on-borrow、test-on-return配置失效</span></span><br><span class="line">      <span class="attr">validation-query:</span> <span class="string">SELECT</span> <span class="number">1</span></span><br><span class="line">      <span class="comment"># 检测连接是否有效的超时时间</span></span><br><span class="line">      <span class="attr">validation-query-timeout:</span> <span class="number">1</span></span><br><span class="line">      <span class="comment"># 检测空闲连接</span></span><br><span class="line">      <span class="comment"># 不影响性能，建议开启</span></span><br><span class="line">      <span class="attr">test-while-idle:</span> <span class="literal">true</span></span><br><span class="line">      <span class="comment"># 检测获取连接时的有效性</span></span><br><span class="line">      <span class="comment"># 开启后会影响性能</span></span><br><span class="line">      <span class="attr">test-on-borrow:</span> <span class="literal">false</span></span><br><span class="line">      <span class="comment"># 检测归还连接时的有效性</span></span><br><span class="line">      <span class="comment"># 开启后会影响性能</span></span><br><span class="line">      <span class="attr">test-on-return:</span> <span class="literal">false</span></span><br><span class="line">      <span class="comment"># 是否开启PSCache，即是否缓存preparedStatement（提升写入、查询效率）</span></span><br><span class="line">      <span class="comment"># 建议在支持游标的数据库开启，例如：Oracle</span></span><br><span class="line">      <span class="attr">pool-prepared-statements:</span> <span class="literal">false</span></span><br><span class="line">      <span class="comment"># 每个连接上PSCache的最大值</span></span><br><span class="line">      <span class="comment"># 如果大于0，pool-prepared-statements自动开启</span></span><br><span class="line">      <span class="attr">max-pool-prepared-statement-per-connection-size:</span> <span class="number">-1</span></span><br><span class="line">      <span class="comment"># 配置默认的监控统计拦截的Filter</span></span><br><span class="line">      <span class="comment"># 不配置则监控页面中的SQL无法统计</span></span><br><span class="line">      <span class="comment"># stat - SQL监控配置</span></span><br><span class="line">      <span class="comment"># wall - SQL防火墙配置</span></span><br><span class="line">      <span class="comment"># slf4j - Druid日志配置</span></span><br><span class="line">      <span class="attr">filters:</span> <span class="string">stat,wall,slf4j</span></span><br><span class="line">      <span class="comment"># 配置过滤器</span></span><br><span class="line">      <span class="attr">filter:</span></span><br><span class="line">        <span class="comment"># SQL监控配置</span></span><br><span class="line">        <span class="attr">stat:</span></span><br><span class="line">          <span class="attr">enabled:</span> <span class="literal">true</span></span><br><span class="line">          <span class="attr">db-type:</span> <span class="string">mysql</span></span><br><span class="line">          <span class="comment"># 是否开启慢SQL统计</span></span><br><span class="line">          <span class="attr">log-slow-sql:</span> <span class="literal">true</span></span><br><span class="line">          <span class="comment"># 慢SQL时间</span></span><br><span class="line">          <span class="attr">slow-sql-millis:</span> <span class="number">10000</span></span><br><span class="line">          <span class="comment"># 慢SQL日志级别</span></span><br><span class="line">          <span class="attr">slow-sql-log-level:</span> <span class="string">ERROR</span></span><br><span class="line">          <span class="comment"># 是否开启合并SQL</span></span><br><span class="line">          <span class="comment"># 开启后，select * from table where id = 1 和 select * from table where id = 2 将合并为 select * from table where id = ?</span></span><br><span class="line">          <span class="attr">merge-sql:</span> <span class="literal">false</span></span><br><span class="line">        <span class="comment"># SQL防火墙配置</span></span><br><span class="line">        <span class="attr">wall:</span></span><br><span class="line">          <span class="attr">enabled:</span> <span class="literal">true</span></span><br><span class="line">          <span class="attr">config:</span></span><br><span class="line">            <span class="comment"># 允许新增</span></span><br><span class="line">            <span class="attr">insert-allow:</span> <span class="literal">true</span></span><br><span class="line">            <span class="comment"># 允许更新</span></span><br><span class="line">            <span class="attr">update-allow:</span> <span class="literal">true</span></span><br><span class="line">            <span class="comment"># 禁止更新时无条件</span></span><br><span class="line">            <span class="attr">update-where-none-check:</span> <span class="literal">true</span></span><br><span class="line">            <span class="comment"># 允许删除</span></span><br><span class="line">            <span class="attr">delete-allow:</span> <span class="literal">true</span></span><br><span class="line">            <span class="comment"># 禁止删除时无条件</span></span><br><span class="line">            <span class="attr">delete-where-none-check:</span> <span class="literal">true</span></span><br><span class="line">            <span class="comment"># 禁止对表ALTER</span></span><br><span class="line">            <span class="attr">alter-table-allow:</span> <span class="literal">false</span></span><br><span class="line">            <span class="comment"># 禁止对表DROP</span></span><br><span class="line">            <span class="attr">drop-table-allow:</span> <span class="literal">false</span></span><br><span class="line">        <span class="comment"># Druid日志配置</span></span><br><span class="line">        <span class="attr">slf4j:</span></span><br><span class="line">          <span class="attr">enabled:</span> <span class="literal">true</span></span><br><span class="line">          <span class="comment"># 关闭数据源日志</span></span><br><span class="line">          <span class="attr">data-source-log-enabled:</span> <span class="literal">false</span></span><br><span class="line">          <span class="comment"># 关闭连接日志</span></span><br><span class="line">          <span class="attr">connection-log-enabled:</span> <span class="literal">false</span></span><br><span class="line">          <span class="comment"># 开启执行SQL日志</span></span><br><span class="line">          <span class="attr">statement-executable-sql-log-enable:</span> <span class="literal">true</span></span><br><span class="line">          <span class="comment"># 开启结果映射日志</span></span><br><span class="line">          <span class="attr">result-set-log-enabled:</span> <span class="literal">true</span></span><br><span class="line">      <span class="comment"># 配置统计页面</span></span><br><span class="line">      <span class="attr">stat-view-servlet:</span></span><br><span class="line">        <span class="attr">enabled:</span> <span class="literal">true</span></span><br><span class="line">        <span class="comment"># 允许重置监控数据</span></span><br><span class="line">        <span class="attr">reset-enable:</span> <span class="literal">true</span></span><br><span class="line">        <span class="comment"># 访问白名单</span></span><br><span class="line">        <span class="attr">allow:</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span></span><br><span class="line">        <span class="comment"># 访问黑名单</span></span><br><span class="line">        <span class="attr">deny:</span> <span class="number">192.168</span><span class="number">.0</span><span class="number">.100</span></span><br><span class="line">        <span class="comment"># 访问用户名</span></span><br><span class="line">        <span class="attr">login-username:</span> <span class="string">druid</span></span><br><span class="line">        <span class="comment"># 访问密码</span></span><br><span class="line">        <span class="attr">login-password:</span> <span class="number">123456</span></span><br><span class="line">      <span class="comment"># 配置统计页面过滤</span></span><br><span class="line">      <span class="attr">web-stat-filter:</span></span><br><span class="line">        <span class="attr">enabled:</span> <span class="literal">true</span></span><br><span class="line">        <span class="comment"># 过滤路径</span></span><br><span class="line">        <span class="attr">url-pattern:</span> <span class="string">/*</span></span><br><span class="line">        <span class="comment"># 排除路径</span></span><br><span class="line">        <span class="attr">exclusions:</span> <span class="string">.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*</span></span><br><span class="line">        <span class="comment"># 开启session统计</span></span><br><span class="line">        <span class="attr">session-stat-enable:</span> <span class="literal">true</span></span><br><span class="line">        <span class="comment"># session统计的最大个数</span></span><br><span class="line">        <span class="attr">session-stat-max-count:</span> <span class="number">100</span></span><br></pre></td></tr></table></figure>

<h2 id="编写"><a href="#编写" class="headerlink" title="编写"></a>编写</h2><p>简单编写查询接口，该接口需要查询数据库，此处略过</p>
<h2 id="系统放行"><a href="#系统放行" class="headerlink" title="系统放行"></a>系统放行</h2><h3 id="Spring-Security"><a href="#Spring-Security" class="headerlink" title="Spring Security"></a>Spring Security</h3><p>配置类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">configure</span><span class="params">(HttpSecurity http)</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">  <span class="comment">// 放行druid相关的资源</span></span><br><span class="line">  http.antMatchers(<span class="string">&quot;/druid/**&quot;</span>).permitAll()</span><br><span class="line">      .......</span><br><span class="line">&#125;    </span><br></pre></td></tr></table></figure>

<h3 id="Shrio"><a href="#Shrio" class="headerlink" title="Shrio"></a>Shrio</h3><p>……</p>
<h2 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h2><h3 id="访问监控页面"><a href="#访问监控页面" class="headerlink" title="访问监控页面"></a>访问监控页面</h3><p>下面为ip地址为yaml中配置的白名单</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="http://127.0.0.1:8724/druid/login.html">http://127.0.0.1:8724/druid/login.html</a></p>
</blockquote>
<p><img src="https://cwh6-bucket.oss-cn-shanghai.aliyuncs.com/bk/image-20231015225454774.png"></p>
<h3 id="监控信息"><a href="#监控信息" class="headerlink" title="监控信息"></a>监控信息</h3><h4 id="客户端向服务器请求"><a href="#客户端向服务器请求" class="headerlink" title="客户端向服务器请求"></a>客户端向服务器请求</h4><blockquote>
<p><a target="_blank" rel="noopener" href="http://localhost:8724/z-self/api/bill/ie/child/cate/list">http://localhost:8724/z-self/api/bill/ie/child/cate/list</a></p>
</blockquote>
<h4 id="SQL监听-amp-定位-amp-分析"><a href="#SQL监听-amp-定位-amp-分析" class="headerlink" title="SQL监听&amp;定位&amp;分析"></a>SQL监听&amp;定位&amp;分析</h4><p> <strong>druid 监控默认配置为每5秒发送请求</strong>，<code>监听</code> 客户端发送给服务器的请求</p>
<p>并记录执行时间，如下：</p>
<p><img src="https://cwh6-bucket.oss-cn-shanghai.aliyuncs.com/bk/image-20231015231649621.png"></p>
<p>点击SQL，也可以清晰看到相关的SQL详情，执行时间等信息</p>
<p><img src="https://cwh6-bucket.oss-cn-shanghai.aliyuncs.com/bk/image-20231015231801078.png"></p>
<p>如果<strong>出现了慢SQL</strong>,根据这SQL去做<code>分析</code>，<strong>索引是否需要添加，索引是否失效，业务SQL是否能优化，数据量的是多少</strong></p>
<blockquote>
<p>参考 <a target="_blank" rel="noopener" href="https://www.cnblogs.com/cao-lei/p/17172886.html">https://www.cnblogs.com/cao-lei/p/17172886.html</a></p>
<p>参考 <a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_37393071/article/details/116325263">https://blog.csdn.net/qq_37393071/article/details/116325263</a></p>
</blockquote>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="https://gitee.com/CWH6">cwh</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="https://gitee.com/CWH6/2023/10/13/%E3%80%90Druid%E3%80%91SpringBoot%E9%85%8D%E7%BD%AEDruid%E7%9B%91%E6%8E%A7%E6%A3%80%E6%B5%8B%E6%85%A2SQL/">https://gitee.com/CWH6/2023/10/13/%E3%80%90Druid%E3%80%91SpringBoot%E9%85%8D%E7%BD%AEDruid%E7%9B%91%E6%8E%A7%E6%A3%80%E6%B5%8B%E6%85%A2SQL/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://cwh6-bucket.oss-cn-shanghai.aliyuncs.com/bk/xhs.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/bk/2023/10/16/%E3%80%90%E6%95%B0%E6%8D%AE%E5%BA%93%E3%80%91%E5%B8%B8%E8%A7%81%E6%95%B0%E6%8D%AE%E5%BA%93%E7%B1%BB%E5%9E%8B/" title="【数据库】常见数据库类型"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">【数据库】常见数据库类型</div></div></a></div><div class="next-post pull-right"><a href="/bk/2023/10/10/%E3%80%90%E5%AE%9E%E6%93%8D%E3%80%91%E5%B8%83%E9%9A%86%E8%BF%87%E6%BB%A4%E5%99%A8%EF%BC%88Bloom-Filter%EF%BC%89%E5%AE%9E%E7%8E%B0%E6%96%87%E5%AD%97%E8%BF%87%E6%BB%A4%E6%8A%80%E6%9C%AF%EF%BC%8C%E8%BF%87%E6%BB%A4%E5%9E%83%E5%9C%BE%E8%AF%84%E8%AE%BA%E5%92%8C%E6%95%8F%E6%84%9F%E8%AF%8D%E6%B1%87/" title="【实操】分词器+布隆过滤器实现文字过滤技术，过滤垃圾评论和敏感词汇"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">【实操】分词器+布隆过滤器实现文字过滤技术，过滤垃圾评论和敏感词汇</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://cwh6-bucket.oss-cn-shanghai.aliyuncs.com/bk/xhs.png" onerror="this.onerror=null;this.src='/bk/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">cwh</div><div class="author-info__description">新博客地址：http://cwh6.gitee.io/vuepressv2/</div></div><div class="card-info-data site-data is-center"><a href="/bk/archives/"><div class="headline">Articles</div><div class="length-num">66</div></a><a href="/bk/tags/"><div class="headline">Tags</div><div class="length-num">33</div></a><a href="/bk/categories/"><div class="headline">Categories</div><div class="length-num">8</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="mailto:2747758546@qq.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a><a class="social-icon" href="https://cwh6-bucket.oss-cn-shanghai.aliyuncs.com/bk/wx_self.jpg" target="_blank" title="wx"><i class="fas fa-qq" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">ありがとう、私の暗い世界の小さな太陽、ありがとう、ずっと温めてくれた</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A6%82%E5%86%B5"><span class="toc-number">1.</span> <span class="toc-text">概况</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%A1%B9%E7%9B%AE%E6%A1%86%E6%9E%B6"><span class="toc-number">2.</span> <span class="toc-text">项目框架</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%95%E5%85%A5"><span class="toc-number">3.</span> <span class="toc-text">引入</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE"><span class="toc-number">4.</span> <span class="toc-text">配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%96%E5%86%99"><span class="toc-number">5.</span> <span class="toc-text">编写</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B3%BB%E7%BB%9F%E6%94%BE%E8%A1%8C"><span class="toc-number">6.</span> <span class="toc-text">系统放行</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Spring-Security"><span class="toc-number">6.1.</span> <span class="toc-text">Spring Security</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Shrio"><span class="toc-number">6.2.</span> <span class="toc-text">Shrio</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95"><span class="toc-number">7.</span> <span class="toc-text">测试</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AE%BF%E9%97%AE%E7%9B%91%E6%8E%A7%E9%A1%B5%E9%9D%A2"><span class="toc-number">7.1.</span> <span class="toc-text">访问监控页面</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%91%E6%8E%A7%E4%BF%A1%E6%81%AF"><span class="toc-number">7.2.</span> <span class="toc-text">监控信息</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%90%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%AF%B7%E6%B1%82"><span class="toc-number">7.2.1.</span> <span class="toc-text">客户端向服务器请求</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SQL%E7%9B%91%E5%90%AC-amp-%E5%AE%9A%E4%BD%8D-amp-%E5%88%86%E6%9E%90"><span class="toc-number">7.2.2.</span> <span class="toc-text">SQL监听&amp;定位&amp;分析</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/bk/2024/02/05/SpringIOC%E7%9A%84%E7%90%86%E8%A7%A3/" title="SpringIOC的理解">SpringIOC的理解</a><time datetime="2024-02-05T14:26:55.000Z" title="Created 2024-02-05 22:26:55">2024-02-05</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/bk/2024/01/18/%E3%80%90ai%E3%80%91chatgpt%E7%BB%95%E5%BC%80%E6%A3%80%E6%9F%A5%E8%BF%87%E6%BB%A4/" title="【ai】chatgpt绕开检查过滤">【ai】chatgpt绕开检查过滤</a><time datetime="2024-01-18T15:55:53.000Z" title="Created 2024-01-18 23:55:53">2024-01-18</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/bk/2024/01/15/%E3%80%90ai%E6%A8%A1%E5%9E%8B%E3%80%91%E5%88%A4%E6%96%AD%E8%AF%AD%E5%8F%A5%E7%9A%84%E7%A7%AF%E6%9E%81%E6%80%A7%E4%B8%8E%E6%B6%88%E6%9E%81%E6%80%A7/" title="【ai模型】判断语句的积极性与消极性">【ai模型】判断语句的积极性与消极性</a><time datetime="2024-01-15T13:10:14.000Z" title="Created 2024-01-15 21:10:14">2024-01-15</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/bk/2023/12/21/%E3%80%90%E5%8C%BA%E5%9D%97%E9%93%BE%E3%80%91%E4%BB%A5%E5%A4%AA%E5%9D%8A%E6%99%BA%E8%83%BD%E5%90%88%E7%BA%A6/" title="【区块链】以太坊智能合约">【区块链】以太坊智能合约</a><time datetime="2023-12-21T13:36:30.000Z" title="Created 2023-12-21 21:36:30">2023-12-21</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/bk/2023/12/21/%E3%80%90Activiti7%E3%80%91%E5%B7%A5%E4%BD%9C%E6%B5%81%E4%BD%BF%E7%94%A8/" title="【Activiti7】工作流使用">【Activiti7】工作流使用</a><time datetime="2023-12-21T13:21:01.000Z" title="Created 2023-12-21 21:21:01">2023-12-21</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By cwh</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/bk/js/utils.js"></script><script src="/bk/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-show-text.min.js" data-mobile="false" data-text="offer" data-fontsize="15px" data-random="false" async="async"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>